﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApShortestPathValidator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Collections.ObjectModel;
using System.Resources;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands;

public class CApShortestPathValidator : ITxValidator
{
  private const string RESOURCE_TABLE_PATH = "DnProcessSimulateCommands.AutomaticDisassembly.ResourceTable";
  private ResourceManager m_resourceManager;

  public CApShortestPathValidator() => this.m_resourceManager = this.GetResourceManger();

  public ITxObject GetObject(string text) => this.GetObject(text, out string _);

  public ITxObject GetObject(string text, out string errorMessage)
  {
    ITxObject itxObject1 = (ITxObject) null;
    errorMessage = (string) null;
    TxObjectList txObjectList = (TxObjectList) null;
    if (text != null)
      txObjectList = TxApplication.ActiveDocument.GetObjectsByName(text);
    if (txObjectList != null && ((Collection<ITxObject>) txObjectList).Count > 0)
    {
      foreach (ITxObject itxObject2 in (Collection<ITxObject>) txObjectList)
      {
        if (this.IsValidObject(itxObject2, out errorMessage))
        {
          itxObject1 = itxObject2;
          break;
        }
      }
    }
    return itxObject1;
  }

  public string GetText(ITxObject obj)
  {
    string text = (string) null;
    ITxPlanningObject planningRepresantation = this.GetPlanningRepresantation(obj);
    if (planningRepresantation != null)
      text = ((ITxObject) planningRepresantation).Name;
    return text;
  }

  public bool IsValidObject(ITxObject obj, out string errorMessage)
  {
    bool flag = true;
    errorMessage = (string) null;
    switch (obj)
    {
      case null:
        flag = false;
        errorMessage = this.m_resourceManager.GetString("NO_SUCH_OBJECT");
        break;
      case TxLogicalCollection _:
      case TxComponent _:
      case TxCompoundPart _:
        if (!this.isUnderDynamicRoot(obj as ITxLogicalChild))
        {
          errorMessage = this.m_resourceManager.GetString("NOT_UNDER_DYNAMIC_ROOT");
          flag = false;
          break;
        }
        if (this.isUnderWorkingUnit(obj as ITxLogicalChild))
        {
          errorMessage = this.m_resourceManager.GetString("CANNOT_REACH_ASSEMBLY_UNIT");
          flag = false;
          break;
        }
        if (obj is TxLogicalCollection && !this.isThereAtLeastOnePhysicalChild(obj as TxLogicalCollection))
        {
          errorMessage = this.m_resourceManager.GetString("NO_PHYSICAL_CHILD");
          flag = false;
          break;
        }
        break;
      default:
        errorMessage = this.m_resourceManager.GetString("WRONG_TYPE");
        flag = false;
        break;
    }
    return flag;
  }

  public bool IsValidText(string text, out string errorMessage)
  {
    return this.GetObject(text, out errorMessage) != null;
  }

  private ITxPlanningObject GetPlanningRepresantation(ITxObject obj)
  {
    switch (obj)
    {
      case ITxProcessModelObject processModelObject:
        pattern_0 = processModelObject.PlanningRepresentation;
        break;
    }
    return pattern_0;
  }

  private ResourceManager GetResourceManger()
  {
    return new ResourceManager("DnProcessSimulateCommands.AutomaticDisassembly.ResourceTable", this.GetType().Assembly);
  }

  private bool isUnderDynamicRoot(ITxLogicalChild obj) => this.isDynamicRoot(obj);

  private bool isDynamicRoot(ITxLogicalChild obj)
  {
    if (obj == TxAutomaticDisassembly.DynamicRoot)
      return true;
    bool flag = false;
    TxObjectList logicalCollections = obj.LogicalCollections;
    switch (obj)
    {
      case TxCompoundPart _:
      case TxComponent _:
        if (((ITxObject) obj).Collection != null)
        {
          ITxObject collection = (ITxObject) ((ITxObject) obj).Collection;
          ((Collection<ITxObject>) logicalCollections).Add(collection);
          break;
        }
        break;
    }
    foreach (ITxObject itxObject in (Collection<ITxObject>) logicalCollections)
    {
      if (itxObject is ITxLogicalChild itxLogicalChild)
        flag = this.isDynamicRoot(itxLogicalChild);
      if (flag)
        break;
    }
    return flag;
  }

  private bool isUnderWorkingUnit(ITxLogicalChild obj) => this.isWorkingUnit(obj);

  private bool isWorkingUnit(ITxLogicalChild obj)
  {
    if (obj == TxAutomaticDisassembly.DynamicRoot)
      return false;
    bool flag = false;
    TxObjectList logicalCollections = obj.LogicalCollections;
    if (obj is TxComponent && ((ITxObject) obj).Collection != null)
    {
      ITxObject collection = (ITxObject) ((ITxObject) obj).Collection;
      ((Collection<ITxObject>) logicalCollections).Add(collection);
    }
    foreach (ITxObject itxObject in (Collection<ITxObject>) logicalCollections)
    {
      if (itxObject is ITxLogicalChild itxLogicalChild)
      {
        TxAutomaticDisassemblyObject disassemblyObject = new TxAutomaticDisassemblyObject((ITxObject) itxLogicalChild);
        if (disassemblyObject != null)
          flag = itxLogicalChild != TxAutomaticDisassembly.DynamicRoot && disassemblyObject.AssemblyUnit || this.isWorkingUnit(itxLogicalChild);
      }
      if (flag)
        break;
    }
    return flag;
  }

  private bool isThereAtLeastOnePhysicalChild(TxLogicalCollection obj)
  {
    return ((Collection<ITxObject>) obj.GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (TxComponent)))).Count > 0;
  }
}
